﻿using fuzhu;
using LH.Model.PU;
using LH.Models.APAR;
using Org.BouncyCastle.Asn1.Ocsp;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using U8COWeb;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.AS;
using XXX.Models.GL;
using XXX.Models.U8Entity;
using ZDEntity;

namespace Yonyou.U8.BO
{
    //取glmodel，为来源参照
    public class CurrentStockVM : CurrentStock
    {
        //当前的数据库连接字符串
        public string conStr { get; set; }
        //public static string cDatabase = "";
          
        public RetModel3<List<CurrentStockQuery>> GetCurrentStock(CurrentStockQuery model)
        {
            try
            {
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {

                    var query = db.Queryable<CurrentStock>()//主表用当前db就行了
                             .InnerJoin<Inventory>((x, y) => x.cInvCode == y.cInvCode)
                             .InnerJoin<ComputationUnit>((x, y, z) => y.cComUnitCode == z.cComunitCode)
                             .InnerJoin<Warehouse>((x, y, z,w) => x.cWhCode == w.cWhCode);
                    ;

                        //查询条件， 仓库编码，存货编码，批次,客户存货编码
                              if (!string.IsNullOrWhiteSpace(DbHelper.GetDbString(model.cWhCode)))
                    {
                        query.Where((x, y, z, w) => x.cWhCode == model.cWhCode);
                    } 
                    if (!string.IsNullOrWhiteSpace(model.cInvCode))
                    {
                        query.Where((x, y, z, w) => y.cInvCode == model.cInvCode);
                    }
                    if (!string.IsNullOrWhiteSpace(model.cBatch))
                    {
                        query.Where((x, y, z, w) => x.cBatch == model.cBatch);
                    }
                    if (!string.IsNullOrWhiteSpace(model.cInvDefine5))
                    {
                        query.Where((x, y, z, w) => y.cInvDefine5 == model.cInvDefine5);
                    }
                    query.Where((x, y, z, w) => x.iQuantity>0);

                    List<CurrentStockQuery> list1 =query.
                        Select((x, y, z, w) => new CurrentStockQuery()
                {
                    cWhCode = x.cWhCode,
                    cWhName = w.cWhName,
                    cInvCode = x.cInvCode,
                    cInvName = y.cInvName,
                    cInvStd = y.cInvStd,
                    cComUnitName = z.cComUnitName,
                    cInvDefine5 =y.cInvDefine5,
                    cFree1 = x.cFree1,
                    cFree2 = x.cFree2,
                    cFree3 = x.cFree3,
                    cFree4 =x.cFree4,
                    iQuantity = x.iQuantity,
                    iNum = x.iNum,
                    fAvaQuantity = x.fAvaQuantity,
                    fOutQuantity  = x.fOutQuantity,
                    cBatch = x.cBatch,
                    dMDate = x.dMdate,
                    dVDate =x.dVDate,
                    fOutNum = x.fOutNum,
                    fAvaNum=x.fAvaNum,
                    fInNum =x.fInNum,
                    fInQuantity = x.fInQuantity,
                    fStopQuantity =x.fStopQuantity,
                   


                }).ToList();

 


                    if (list1 != null)
                    {
                        return DBService.ReOK3(list1);
                    }
                    else
                    {
                        return DBService.ReError3<List<CurrentStockQuery>>("查询不到此入库号");
                    }

                }
            }
            catch (Exception ex)
            { 
                return DBService.ReError3<List<CurrentStockQuery>>(ex.Message);
            }
        }
    }
}
